package com.abc.recycling.mapper;

import com.abc.recycling.model.Notification;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.time.LocalDateTime;

@Mapper
public interface NotificationMapper {
    
    @Select("SELECT * FROM notification WHERE id = #{id}")
    Notification findById(@Param("id") Long id);
    
    @Select("SELECT * FROM notification WHERE receiver_id = #{userId} " +
            "ORDER BY create_time DESC")
    List<Notification> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM notification WHERE type = 'SYSTEM' " +
            "ORDER BY create_time DESC")
    List<Notification> findSystemNotifications();
    
    @Select("SELECT COUNT(*) FROM notification WHERE receiver_id = #{userId} " +
            "AND status = 0")
    long countUnreadByUserId(@Param("userId") Long userId);
    
    @Insert("INSERT INTO notification (title, content, type, receiver_id, receiver_type, " +
            "status, is_sent, create_time) " +
            "VALUES (#{title}, #{content}, #{type}, #{receiverId}, #{receiverType}, " +
            "#{status}, #{isSent}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Notification notification);
    
    @Update("UPDATE notification SET status = #{status}, is_sent = #{isSent}, " +
            "send_time = #{sendTime}, read_time = #{readTime}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    void update(Notification notification);
    
    @Delete("DELETE FROM notification WHERE id = #{id}")
    void deleteById(@Param("id") Long id);
    
    @Select("SELECT * FROM notification WHERE receiver_type = #{receiverType} " +
            "AND create_time >= #{startTime} ORDER BY create_time DESC")
    List<Notification> findByReceiverType(@Param("receiverType") String receiverType, 
            @Param("startTime") LocalDateTime startTime);
} 